import java.util.ArrayList;
import java.util.List;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode root;//指向一个根节点
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    public void preOrder(TreeNode root){
        if (root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root){
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    public void lastOrder(TreeNode root){
        if (root == null) {
            return;
        }
        lastOrder(root.left);
        lastOrder(root.right);
        System.out.print(root.val+" ");
    }
    List<TreeNode> ret = new ArrayList<>();
    public List<TreeNode> preOrder2(TreeNode root){ //将前序遍历的结果存储到List中
        if (root == null) {
            return ret;
        }
        ret.add(root);
        List<TreeNode> leftTree = preOrder2(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree = preOrder2(root.right);
        ret.addAll(rightTree);
        return ret;
    }
    public int size = 0;
    public void nodeSize(TreeNode root){ //计算该二叉树中的所有节点数
        if (root == null) {
            return ;
        }
        size++;
        nodeSize(root.left);
        nodeSize(root.right);
    }

    public int nodeSize2(TreeNode root){
        if (root == null) {
            return 0;
        }
        return nodeSize2(root.left) + nodeSize2(root.right) + 1;
    }
    public int leafSize = 0;
    public void getLeafSize(TreeNode root){ //叶子节点的个数
        if (root == null) {
            return;
        }
        if (root.left ==null && root.right == null){
            leafSize++;
        }
        getLeafSize(root.left);
        getLeafSize(root.right);
    }
    public int getLeafSize2(TreeNode root){
        if (root == null) {
            return 0;
        }
        if (root.left ==null && root.right == null){
            return 1;
        }
        return getLeafSize2(root.left) + getLeafSize2(root.right);
    }

    public int getKLevelNodeCount(TreeNode root,int k){ //第k层叶子的节点个数
        if (root == null) {
            return 0;
        }
        if (k == 1){ //第k层的节点相对与自己来说是第一层
            return 1;
        }
        //如果不是1，则继续向下层遍历，并且返回左树和右树的第k层叶子节点和
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }
    public int height = 0;
    public int lower = 0;
    public int getHeight(TreeNode root){ //计算树的层数
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return (leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1);
        //代码走到树的最底层开始加1，一直向上返回即可得到累计层数加1的值
    }
}
